#!/usr/bin/ruby

Commands.add_command(:cpu,Proc.new do |no|
                       #cpu  33137 423 9721 661160 35971 202 359 0 0
                       cpu_line_1=File.readlines("/proc/stat")[0]
                       sleep 0.1
                       cpu_line_2=File.readlines("/proc/stat")[0]

                       @@log.info cpu_line_1
                       @@log.info cpu_line_2


                       cpu_params_1=cpu_line_1.split
                       cpu_params_1.shift

                       cpu_params_2=cpu_line_2.split
                       cpu_params_2.shift


                       total_1=0
                       @@log.info 'cpu_params_1'
                       cpu_params_1.each do |value|
                         @@log.info value
                         total_1+=value.to_f
                       end

                       total_2=0
                       @@log.info 'cpu_params_2'
                       cpu_params_2.each do |value|
                         @@log.info value
                         total_2+=value.to_f
                       end

                       total_diff=total_2-total_1
                       idle_diff=cpu_params_2[3].to_f-cpu_params_1[3].to_f
                       sys_idle=idle_diff/total_diff*100
                       ("%.2f"%(100-sys_idle)).to_s
                     end)
